<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class admincontrol extends control {
	
	public function __construct() {
		$aC = $this->_chechlogin(); //检查用户登陆
		all::$aU = model::member()->get( $aC['id'] );
	}
	
	public function onindex() {
		if ( 2 > all::$aU['power'] ) { func::showerr( '对不起，此板块限管理员可进入!' ); }

		all::$aOut['sms_num'] = model::sms()->checknew( all::$aU['id'] );
		all::$aOut['mod_name'] = all::$cfg['navname']['admin']; //模块名称
		all::$aOut['top10'] = model::member()->gettop10();//输出班级会员积分 TOP 10
		all::$aOut['mbs'] = model::member()->getalluser(); //输出班集所有成员
		func::display( all::$aOut, 'admin.htm' );
	}
	
	public function onmember() {
		$do = func::input ( 'do' );
		$uid = func::input ( 'id' );
		switch ( $do ) {
			case 'pass'://批准加入
				model::member()->pass( $uid );
				break;
			case 'power_up'://提升为管理员
				model::member()->updatepower( $uid );
				break;
			case 'power_down'://降级为普通会员
				model::member()->updatepower( $uid, false );
				break;
			case 'reg_out' ://踢出本班
				model::member()->out( $uid );
				break;
		}
		
		func::redirect( "./index.php?m=admin" );
	}
	
	public function onindex2() {
		if ( 4 > all::$aU['power'] ) { func::showerr( '对不起，您的权限不够!' ); }
		
		all::$aOut['sms_num'] = model::sms()->checknew( all::$aU['id'] );
		all::$aOut['mod_name'] = all::$cfg['navname']['admin']; //模块名称
		all::$aOut['top10'] = model::member()->gettop10();//输出班级会员积分 TOP 10

		func::display( all::$aOut, 'admin2.htm' );
	}
	
	public function onup() { //配置班集信息
		$do = func::input( 'do' );   //配置同学录
		$needpower = 4;
		$admin = 'index2';
		if( in_array( $do, array( 'up_brief', 'up_album_brief' ) ) ) {
			$needpower = 2;
			$admin = 'index';
		}
		if ( $needpower > all::$aU['power'] ) { func::showerr( '对不起，您的权限不够!' ); }
		
		/**
		 * 班级信息配置文件
		 */
		$class_config_template = '<?php
/* 同学录配置文件 */

//班级宣言
$cfg[\'class\'][\'brief\']          = \'%s\';
//相册宣言
$cfg[\'class\'][\'album_brief\']    = \'%s\';
//班级名称
$cfg[\'class\'][\'c_name\']         = \'%s\';
//入学年份
$cfg[\'class\'][\'c_year\']         = \'%d\';

//同学录创建者
$cfg[\'class\'][\'creator\']        = \'%s\';
//创建者姓名
$cfg[\'class\'][\'creator_name\']   = \'%s\';
//同学录创建时间
$cfg[\'class\'][\'create_time\']    = \'%s\';

//同学录当前皮肤
$cfg[\'class\'][\'skin\']           = \'%s\';

//同学录班级留言每页的留言数
$cfg[\'class\'][\'words_per_page\'] = %d;

//同学录相册容量,单位为MB
$cfg[\'class\'][\'album_volume\']   = %d;
//上传的相片最大大小,单位为KB
$cfg[\'class\'][\'pix_max_size\']   = %d;
//同学录相册每页显示照片数,为5的倍数
$cfg[\'class\'][\'pix_per_page\']   = %d;

//同学录班级共享容量,单位为MB
$cfg[\'class\'][\'share_volume\']   = %d;
//同学录上传的共享文件最大大小,单位为KB
$cfg[\'class\'][\'file_max_size\']  = %d;
//同学录班级共享每页列文件数
$cfg[\'class\'][\'file_per_page\']  = %d;

//同学录新加入同学是否要经批准? 1-是;0-否
$cfg[\'class\'][\'confirm_new\']    = %d;

//同学录留言簿是否公开(外人也能看见)
$cfg[\'class\'][\'open_words\']     = %d;
//同学录相册是否公开
$cfg[\'class\'][\'open_album\']     = %d;
//同学录班级共享是否公开
$cfg[\'class\'][\'open_share\']     = %d;
//同学录地址簿是否公开
$cfg[\'class\'][\'open_addr\']      = %d;

';
		if ( $do ) { //同学录配置
			if ( $do == 'up_brief' ) {
				$brief = func::input( 'brief', 's', false );
				if ( empty( $brief ) ) { return false ; }
				all::$cfg['class']['brief'] = $brief;
			} elseif ( $do == 'up_album_brief' ) {
				$album_brief = func::input( 'album_brief', 's', false );
				if ( empty( $album_brief ) ) { return false ; }
				all::$cfg['class']['album_brief'] = $album_brief;
			} elseif ( $do == 'up_class' ) {
				//c_name,c_year,creator,creator_name,creator_time,words_per_page
				$c_name         = func::input( 'c_name' );               //班级名称
				$c_year         = func::input( 'c_year', 'i' );          //班级成立时间
				if ( $c_year < 0 ) { return false ; }
				$creator        = func::input( 'creator' );              //班级创始人ID
				$creator_name   = func::input( 'creator_name' );         //班级创始人
				$create_time    = func::input( 'create_time' );          //班级创建时间
				$words_per_page = func::input( 'words_per_page', 'i' );  //每页显示的留言条数
				if ( $words_per_page < 0 ) { return false; }

				//检验信息是否为空
				if ( empty( $c_name ) || empty( $c_year ) || empty( $creator ) ||
				empty( $creator_name ) || empty( $create_time ) || empty( $words_per_page ) ) { return false ; }
				all::$cfg['class']['c_name']         = $c_name;
				all::$cfg['class']['c_year']         = $c_year;
				all::$cfg['class']['creator']        = $creator;
				all::$cfg['class']['creator_name']   = $creator_name;
				all::$cfg['class']['create_time']    = $create_time;
				all::$cfg['class']['words_per_page'] = $words_per_page;

			} elseif ( $do == 'up_album' ) {
				//$album_volume $pix_max_size $pix_per_page
				$album_volume = func::input( 'album_volume', 'i' );
				$pix_max_size = func::input( 'pix_max_size', 'i' );
				$pix_per_page = func::input( 'pix_per_page', 'i' );
				if ( $pix_max_size < 0 || $pix_max_size > 2048 ) { return false; } //允许上传相片大小不能为负，不能大于2M
				if ( $pix_per_page < 0 || $album_volume < 0 ) { return false; }    //每页相片数和相片空间总容量不能为负
				
				if ( empty( $album_volume ) || empty( $pix_max_size ) || empty( $pix_per_page ) ) { return false; }
				all::$cfg['class']['album_volume'] = $album_volume;
				all::$cfg['class']['pix_max_size'] = $pix_max_size;
				all::$cfg['class']['pix_per_page'] = $pix_per_page;

			} elseif ( $do == 'up_share' ) {
				//share_volume file_max_size file_per_page
				$share_volume  = func::input( 'share_volume', 'i' );
				$file_max_size = func::input( 'file_max_size', 'i' );
				$file_per_page = func::input( 'file_per_page', 'i' );
				if ( $file_max_size < 0 || $file_max_size > 2048 ) { return false; } //允许上传文件大小不能为负，不能大于2M
				if ( $file_per_page < 0 || $share_volume < 0 ) { return false; }     //每页文件数和文件空间总容量不能为负

				if ( empty( $share_volume ) || empty( $file_max_size ) || empty( $file_per_page ) ) { return false; }
				all::$cfg['class']['share_volume']  = $share_volume;
				all::$cfg['class']['file_max_size'] = $file_max_size;
				all::$cfg['class']['file_per_page'] = $file_per_page;

			} elseif ( $do == 'up_open' ) {
				//confirm_new open_words open_album open_share open_addr
				$confirm_new = ( func::input( 'confirm_new', 'i' ) > 0 ) ? 1 : 0 ;
				$open_words  = ( func::input( 'open_words', 'i'  ) > 0 ) ? 1 : 0 ;
				$open_album  = ( func::input( 'open_album', 'i'  ) > 0 ) ? 1 : 0 ;
				$open_share  = ( func::input( 'open_share', 'i'  ) > 0 ) ? 1 : 0 ;
				$open_addr   = ( func::input( 'open_addr', 'i'   ) > 0 ) ? 1 : 0 ;

				all::$cfg['class']['confirm_new'] = $confirm_new;
				all::$cfg['class']['open_words']  = $open_words;
				all::$cfg['class']['open_album']  = $open_album;
				all::$cfg['class']['open_share']  = $open_share;
				all::$cfg['class']['open_addr']   = $open_addr;

			} else {
				func::redirect( "./index.php?m=admin&a=index2" );
			}

			//格式化配置文件，实质成为字符串流
			$new_config = sprintf ( $class_config_template,
			all::$cfg['class']['brief'],
			all::$cfg['class']['album_brief'],
			all::$cfg['class']['c_name'],
			all::$cfg['class']['c_year'],
			all::$cfg['class']['creator'],
			all::$cfg['class']['creator_name'],
			all::$cfg['class']['create_time'],
			all::$cfg['class']['skin'],
			all::$cfg['class']['words_per_page'],
			all::$cfg['class']['album_volume'],
			all::$cfg['class']['pix_max_size'],
			all::$cfg['class']['pix_per_page'],
			all::$cfg['class']['share_volume'],
			all::$cfg['class']['file_max_size'],
			all::$cfg['class']['file_per_page'],
			all::$cfg['class']['confirm_new'],
			all::$cfg['class']['open_words'],
			all::$cfg['class']['open_album'],
			all::$cfg['class']['open_share'],
			all::$cfg['class']['open_addr']
			);

			file_put_contents( DIR_DAT . 'class.info.php', $new_config ); //将新配置写入文件
		}
		
		func::redirect( "./index.php?m=admin&a=$admin" );
	}
}